using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Microsoft.MediaCenter;
using Microsoft.MediaCenter.UI;

namespace MCEMediaManager
{
    public interface IMediaPlugin : IModelItem
    {
        string Name{get;set;}
        int SearchResultsCount { get; }
        string SearchLabel { get; set; }
        string DiscoverContentLabel { get; set; }
        IList Commands { get; set; }
        IList GetDiscoveryItems();
        IList GetDiscoveryItems(string filter);
        IList GetTVItems();
        IList GetTVItems(string filter);
        IList GetSearchResults();
        IList GetSearchResults(string filter);
        void OnDetailsActionInvoked(DetailsCommand command);
        ICommand CreateCommand(IMediaItem theMediaItem, string description, string type, string value);
        bool HasSearchInterface { get; }
        bool HasDiscoveryInterface { get; }
        bool HasTVInterface { get; }
        void SearchByName(object obj);
        void CreateDiscoveryItems(object obj);
        void CreateTVItems(object obj);
        List<String> Filters{get;set;}
        bool HasRatings { get; }
        int RatingsScale { get; }
        ICommand CreateRatingsCommand(IMediaItem mediaItem, int ratingNumber);
        void OnSetRatingInvoked(DetailsCommand command);
    }

    public class MediaPlugin : ModelItem, IMediaPlugin
    {
        private string _Name;
        protected int _SearchResultsCount = 0;
        protected int _DiscoveryItemsCount = 0;
        private string _SearchLabel = "";
        private string _DiscoverContentLabel = "";
        private List<String> _Filters;

        public virtual string Name
        {
            get { return (this._Name); }
            set 
            {
                if (this._Name != value)
                {
                    this._Name = value;
                    FirePropertyChanged("Name");
                }
            }
        }

        public virtual int SearchResultsCount
        {
            get { return (this._SearchResultsCount); }
            set
            {
                if (this._SearchResultsCount != value)
                {
                    this._SearchResultsCount = value;
                    FirePropertyChanged("SearchResultsCount");
                    FirePropertyChanged("SearchLabel");
                }
            }
        }

        public virtual int DiscoveryItemsCount
        {
            get { return (this._DiscoveryItemsCount); }
            set
            {
                if (this._DiscoveryItemsCount != value)
                {
                    this._DiscoveryItemsCount = value;
                    FirePropertyChanged("DiscoveryItemsCount");
                    FirePropertyChanged("DicoverContentLabel");
                }
            }
        }

        public virtual string SearchLabel
        {
            get 
            {
                if (this._SearchLabel == "")
                    return this.Name + " (" + this.SearchResultsCount + ")";
                else
                    return this._SearchLabel;
            }
            set
            {
                if (this._SearchLabel != value)
                {
                    this._SearchLabel = value;
                    FirePropertyChanged("SearchLabel");
                }
            }
        }

        public virtual string DiscoverContentLabel
        {
            get
            {
                if (this._DiscoverContentLabel == "")
                    return this.Name + " (" + this.DiscoveryItemsCount  + ")";
                else
                    return this._DiscoverContentLabel;
            }
            set
            {
                if (this._DiscoverContentLabel != value)
                {
                    this._DiscoverContentLabel = value;
                    FirePropertyChanged("DiscoverContentLabel");
                }
            }
        }

        private IList _Commands;

        public virtual IList Commands
        {
            get { return (this._Commands); }
            set { this._Commands = value; }
        }

        public virtual IList GetDiscoveryItems(string filter)
        {
            return new ArrayListDataSet(this);
        }

        public virtual IList GetTVItems(string filter)
        {
            return new ArrayListDataSet(this);
        }

        public virtual IList GetTVItems()
        {
            return GetTVItems("");
        }

        public virtual IList GetDiscoveryItems()
        {
            return GetDiscoveryItems("");
        }

        public virtual IList GetSearchResults(string filter)
        {
            return new ArrayListDataSet(this);
        }

        public virtual IList GetSearchResults()
        {
            return GetSearchResults("");
        }

        public virtual List<String> InitializeFilters()
        {
            List<string> tempFilters = new List<string>();
            tempFilters.Add("All");
            return tempFilters;
        }

        public List<String> Filters
        {
            get 
            {
                if (_Filters == null)
                {
                    _Filters = InitializeFilters();
                    FirePropertyChanged("Filters");
                }
                return (this._Filters);
            }
            set
            {
                if (_Filters != value)
                {
                    _Filters = value;
                    FirePropertyChanged("Filters");
                }
            }
        }

        /// <summary>
        /// Create a single details page command.
        /// </summary>
        public ICommand CreateCommand(IMediaItem theMediaItem, string description, string type, string value)
        {
            DetailsCommand command = new DetailsCommand(theMediaItem, description);
            command.Type = type;
            command.Value = value;

            // Hook up the event handler.  This handler will disambiguate
            // what the actual action that should occur based on the item's
            // action type.
            command.Invoked += delegate(object sender, EventArgs e)
            {
                OnDetailsActionInvoked((DetailsCommand)sender);
            };

            return command;
        }


        /// <summary>
        /// Called when one of the custom actions on a details page is clicked.
        /// </summary>
        public virtual void OnDetailsActionInvoked(DetailsCommand command)
        {
            Application.Current.MessageBox("This Action has not been implemented!", "Information", 2, true);
        }

        /// <summary>
        /// Call when someone picks a rating for this item.
        /// </summary>
        /// <param name="command">The ratings command</param>
        public virtual void OnSetRatingInvoked(DetailsCommand command)
        {
            ((IMediaItem)command.Owner).UserRating = command.ItemId;
            //Application.Current.MessageBox("Rating : " + newRating + "\r\nThe ratings system for this plugin has not been implemented yet!", "Information", 2, true);
        }

        /// <summary>
        /// Create a ratings command button.  There are as many of these created as there are ratings scale.
        /// </summary>
        public ICommand CreateRatingsCommand(IMediaItem theMediaItem, int ratingNumber)
        {
            DetailsCommand command = new DetailsCommand(theMediaItem, ratingNumber.ToString());
            command.ItemId = ratingNumber;

            // Hook up the event handler.  This handler will disambiguate
            // what the actual action that should occur based on the item's
            // action type.
            command.Invoked += delegate(object sender, EventArgs e)
            {
                OnSetRatingInvoked((DetailsCommand)sender);
            };

            return command;
        }

        public virtual bool HasSearchInterface { get { return false; } }

        public virtual bool HasDiscoveryInterface { get { return false; } }

        public virtual bool HasTVInterface { get { return false; } }

        public virtual bool HasRatings { get { return false; } }

        public virtual int RatingsScale { get { return 5; } }

        public virtual void SearchByName(object obj) { }

        public virtual void CreateDiscoveryItems(object obj) { }

        public virtual void CreateTVItems(object obj) { }
    }
}
